package cloudresource

import (
	smn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2"
	smnModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/model"
	"scase.io/application-auto-scaling-service/pkg/api/model"
	"scase.io/application-auto-scaling-service/pkg/utils/logger"
)

type SmnResourceController struct {
	smnClient *smn.SmnClient
	projectId string
	tLogger   *logger.FMLogger
}

func NewSmnResourceController(projectId string, tLogger *logger.FMLogger) (*SmnResourceController, error) {
	resourceController, err := GetResourceController(projectId)
	if err != nil {
		return nil, err
	}
	return &SmnResourceController{
		smnClient: resourceController.smnClient,
		projectId: projectId,
		tLogger:   tLogger,
	}, nil
}

// CreateTopic: 在smn上创建云资源
func (c *SmnResourceController) CreateTopic(param *model.CreateTopicRequest) (
	*smnModel.CreateTopicResponse, error) {
	req := &smnModel.CreateTopicRequest{
		Body: &smnModel.CreateTopicRequestBody{
			Name:        param.Name,
			DisplayName: param.DisplayName,
		},
	}
	// 企业项目为空时不传该参数，使用默认企业项目
	if param.EnterpriseProjectId != "" {
		req.Body.EnterpriseProjectId = &param.EnterpriseProjectId
	}
	return c.smnClient.CreateTopic(req)
}

// ListTopicByEpAndName: 根据企业项目和主题名获取topic列表
func (c *SmnResourceController) ListTopicByEpAndName(epId string, name string) (
	*smnModel.ListTopicsResponse, error) {
	req := &smnModel.ListTopicsRequest{
		Name: &name,
	}
	if epId != "" {
		req.EnterpriseProjectId = &epId
	}
	return c.smnClient.ListTopics(req)
}

// DeleteTopic: 在smn上删除云资源
func (c *SmnResourceController) DeleteTopic(topicUrn string) (
	*smnModel.DeleteTopicResponse, error) {
	req := &smnModel.DeleteTopicRequest{
		TopicUrn: topicUrn,
	}
	return c.smnClient.DeleteTopic(req)
}

// AddSubscription: 对某一主题添加订阅
func (c *SmnResourceController) AddSubscription(topicUrn string, param *model.AddSubscriptionRequest) (
	*smnModel.AddSubscriptionResponse, error) {
	req := &smnModel.AddSubscriptionRequest{
		TopicUrn: topicUrn,
		Body: &smnModel.AddSubscriptionRequestBody{
			Protocol: param.Protocol,
			Endpoint: param.Endpoint,
			Remark:   &param.Remark,
		},
	}
	return c.smnClient.AddSubscription(req)
}

// DeleteSubscription: 对某一主题取消订阅
func (c *SmnResourceController) CancelSubscription(subcriptionUrn string) (
	*smnModel.CancelSubscriptionResponse, error) {
	req := &smnModel.CancelSubscriptionRequest{
		SubscriptionUrn: subcriptionUrn,
	}
	return c.smnClient.CancelSubscription(req)
}

// ListSubscriptionsByTopic: 查询订阅者列表
func (c *SmnResourceController) ListSubscriptionsByTopic(topicUrn string, limit, offset int32) (
	*smnModel.ListSubscriptionsByTopicResponse, error) {
	paramOffset := int32(offset * limit)
	req := &smnModel.ListSubscriptionsByTopicRequest{
		TopicUrn: topicUrn,
		Limit:    &limit,
		Offset:   &paramOffset,
	}
	return c.smnClient.ListSubscriptionsByTopic(req)
}

// PublishMessage: 发布消息
func (c *SmnResourceController) PublishMessage(topicUrn string, subject string, message string) (
	*smnModel.PublishMessageResponse, error) {
	req := &smnModel.PublishMessageRequest{
		TopicUrn: topicUrn,
		Body: &smnModel.PublishMessageRequestBody{
			Subject: &subject,
			Message: &message,
		},
	}
	return c.smnClient.PublishMessage(req)
}
